#!/bin/bash
#
#cal some project someone edit lines
#
#
#********************某个项目最近24小时增加减少的代码量********************
#
#project_path="$1"                                        #项目路径
m_date=`date +%Y-%m-%d -d "1 days ago"`                   #统计的日期
cal_time="1.days"                                         #统计时长
log_path="/var/log/git.log"
#
#cd $project_path
project_name=`basename $PWD`
#git pull
#输入用户名/SSH key
lines=`git log --stat --since="$cal_time" | grep -n ^commit | awk 'BEGIN{FS=":| " }{print $1}'`
commits_num=`echo $lines |wc -w `
lines=($lines)
num=0
if [ $commits_num = 0 ];then
    echo "`date` $project_name 没有提交" >> $log_path
    exit 11
fi
#
all_lines=`git log --stat --since="$cal_time"|wc -l`
for con in `seq 1 ${commits_num}`;do
       if [[ $con == $commits_num ]];then
               version_num=`git log --stat --since="$cal_time" | sed -n ${lines[$con]}p | head -1 | cut -d " " -f2`
               author_user=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${all_lines}p | head -2 | tail -1 | cut -d " " -f 2`
               commit_date=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${all_lines}p | head -3 | tail -1 | awk 'BEGIN{FS=" "}{print $6,$3,$4,$5}'`
               author_mail=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${all_lines}p | head -2 | tail -1 | awk 'BEGIN{FS="<|>"}{print $2}'`
               change_lines=`git log --stat --since="$cal_time" | tail -1 |  awk '{print $1,$4,$6}'`
               change_lines=($change_lines)
               edit_file_num=${change_lines[0]}
               add_lines_num=${change_lines[1]}
               del_lines_num=${change_lines[2]}
               echo -e "${commit_date}\t${project_name}\t${version_num}\t${author_user}\t${author_mail}\t${edit_file_num}\t${add_lines_num}\t${del_lines_num}" >> $log_path
	       mysql -h 192.168.0.227 -u icms -pqwe123 -D CodeCal <<EOF
        	insert into GitCal (commit_time,project_name,user,version_num,email,files,add_lines,del_lines) values ("$commit_date","$project_name","$author_user","$version_num","$author_mail","$edit_file_num","$add_lines_num","$del_lines_num")
EOF
       else
               version_num=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -1 | cut -d " " -f2`
               author_user=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -2 | tail -1 | cut -d " " -f 2`
               commit_date=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -3 | tail -1 | awk 'BEGIN{FS=" "}{print $6,$3,$4,$5}'`
               author_mail=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | head -2 | tail -1 | awk 'BEGIN{FS="<|>"}{print $2}'`
               change_lines=`git log --stat --since="$cal_time" | sed -n ${lines[$num]},${lines[$con]}p | tail -3 | head -1 | awk '{print $1,$4,$6}'`
               change_lines=($change_lines)
               edit_file_num=${change_lines[0]}
               add_lines_num=${change_lines[1]}
               del_lines_num=${change_lines[2]}
               echo -e "${commit_date}\t${project_name}\t${version_num}\t${author_user}\t${author_mail}\t${edit_file_num}\t${add_lines_num}\t${del_lines_num}" >> $log_path
#                         提交时间     项目名称        版本号          提交用户        用户邮箱        文件变化数         增加行数             删除行数
	       mysql -h 192.168.0.227 -u icms -pqwe123 -D CodeCal <<EOF
        	insert into GitCal (commit_time,project_name,user,version_num,email,files,add_lines,del_lines) values ("$commit_date","$project_name","$author_user","$version_num","$author_mail","$edit_file_num","$add_lines_num","$del_lines_num")
EOF
       fi
       num=$((${num}+1))
done
